package com.wp.controller.impl;

import com.wp.entity.StorageRecord;
import com.wp.entity.Supplier;
import com.wp.entity.SysRole;
import com.wp.entity.SysUser;
import com.wp.service.StorageRecordService;
import com.wp.service.SupplierService;
import com.wp.service.SysRoleService;
import com.wp.service.SysUserService;
import com.wp.utils.Constants;
import com.wp.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/storageRecord")
public class StorageRecordController {

    // 用户
    @Resource
    private StorageRecordService storageRecordService;
    // 角色
    @Resource
    private SupplierService supplierService;

    @RequestMapping("/list")
    public String userList(@RequestParam(value = "pageIndex", required = false) String pageIndex,
                           @RequestParam(value = "queryGoodsName", required = false) String queryGoodsName,
                           @RequestParam(value = "querySupplierId", required = false) Integer querySupplierId,
                           @RequestParam(value = "querypayStatus", required = false) Integer querypayStatus,
                           Model model, HttpSession session) throws Exception {
        //默认页码
        Integer currentPageNo = 1;
        //每页显示的记录数
        Integer pageSize = Constants.pageSize;

        //从请求的参数中获取pageIndex，并转换为当前页码
        if (pageIndex != null && !"".equals(pageIndex)) {
            currentPageNo = Integer.valueOf(pageIndex);
        }

        //获取总记录数
        Integer totalCount = storageRecordService.findRecordTotalCount(queryGoodsName,querySupplierId,querypayStatus);
        //计算总页数
        PageSupport pageSupport = new PageSupport();
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);
        //总页数
        Integer totalPageCount = pageSupport.getTotalPageCount();
        //控制页码
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        //获取业务层中的用户列表
        List<StorageRecord> storageRecordList = storageRecordService.findRecordByPage(currentPageNo, pageSize,queryGoodsName,querySupplierId,querypayStatus);
        List<Supplier> supplierList = supplierService.findAll();
        SysUser sysUser = (SysUser) session.getAttribute("UserSession");
        model.addAttribute("storageRecordList", storageRecordList);
        model.addAttribute("supplierList", supplierList);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("queryGoodsName", queryGoodsName);
        model.addAttribute("querySupplierId", querySupplierId);
        model.addAttribute("querypayStatus", querypayStatus);
        model.addAttribute("sysuserid", sysUser.getId());
        //逻辑视图名
        return "storageRecord/list";
    }

    // 跳转到jsp添加页面
    @RequestMapping("/toAdd")
    public String toAdd(StorageRecord storageRecord){

        return "storageRecord/add";
    }

    // 查询供货商
    @RequestMapping("/simpleList")
    @ResponseBody
    public List<Supplier> simpleList(){
        List<Supplier> supplierId = supplierService.findAll();

        return supplierId;
    }

    // 添加信息
    @RequestMapping("/add")
    public String add(StorageRecord storageRecord){

        int add = storageRecordService.toAdd(storageRecord);

        if (add>0){
            return "redirect:/sys/user/list";
        }
        return "sysUser/add";
    }

    // 查看个人信息
    @RequestMapping("/view/{storageRecordId}")
    public String view(Model model,@PathVariable(name = "storageRecordId") Integer id){

        StorageRecord storageRecord = storageRecordService.findById(id);

        model.addAttribute("storageRecord",storageRecord);

        return "storageRecord/view";
    }

    // 删除信息
    @DeleteMapping("/del/{storageRecordId}")
    @ResponseBody // 页面不跳转》》用于异步请求
    public String del(@PathVariable (name = "storageRecordId") Integer id) {

        int del = storageRecordService.del(id);

        if (del > 0) {
            return "{\"delResult\":\"true\"}";
        } else if(del<0){
            return "{\"delResult\":\"false\"}";
        }else{
            return null;
        }
    }

    // 查询个人信息在修改
    @RequestMapping("/toUpdate/{storageRecordId}")
    public String toUpdate(Model model,@PathVariable(name = "storageRecordId") Integer id) {

        StorageRecord storageRecord = storageRecordService.findById(id);

        model.addAttribute("storageRecord",storageRecord);

        return "storageRecord/update";
    }

    // 修改个人信息
    @PostMapping("/upd")
    public String upd(StorageRecord storageRecord) {

        int  upd = storageRecordService.upd(storageRecord);

        if (upd>0){
            return "redirect:/sys/user/list";
        }
        return null;
    }

}
